package com.pussinboots.morning.cms.modules.order.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pussinboots.morning.cms.modules.system.category.dto.OsCategoryDto;
import com.pussinboots.morning.cms.modules.system.category.service.IOsCategoryService;
import com.pussinboots.morning.cms.modules.system.order.entity.OsOrder;
import com.pussinboots.morning.cms.modules.system.order.entity.OsOrderProduct;
import com.pussinboots.morning.cms.modules.system.order.entity.OsOrderShipment;
import com.pussinboots.morning.cms.modules.system.order.entity.OsOrderStatus;
import com.pussinboots.morning.cms.modules.system.order.service.IOsOrderProductService;
import com.pussinboots.morning.cms.modules.system.order.service.IOsOrderService;
import com.pussinboots.morning.cms.modules.system.order.service.IOsOrderShipmentService;
import com.pussinboots.morning.cms.modules.system.order.service.IOsOrderStatusService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductService;
import com.pussinboots.morning.cms.modules.system.version.dto.VersionLogDTO;
import com.pussinboots.morning.cms.modules.system.version.service.IVersionLogService;
import com.pussinboots.morning.common.controller.BaseController;
import com.pussinboots.morning.common.model.PageInfo;

/**
 * 
* 项目名称：morning-cms-webapp   
* 类名称：SystemVersionLogControlller   
* 类描述：版本日至表示层控制器    
* 创建人：陈星星   
* 创建时间：2017年2月17日 上午12:32:07
 */
@Controller
@RequestMapping(value = "/system/order")
public class OrderControlller extends BaseController {
	
	/** 版本日志 */
	private static final String SYSTEM_ORDER_LIST = getViewPath("modules/order/system_order_list");
	
	/** 版本日志 */
	private static final String SYSTEM_ORDER_LIST_VIEW = getViewPath("modules/order/system_order_list_view");
	
	
	/** 版本日志 */
	private static final String SYSTEM_VERSION_LIST = getViewPath("modules/version/system_version_list");
	/** 分页日志 */
	private static final String SYSTEM_VERSION_PAGE = getViewPath("modules/version/system_version_page");
	
	@Autowired
	private IVersionLogService versionLogService;
	
	@Autowired
	private IOsProductService productService;
	@Autowired
	private IOsCategoryService categoryService;
	
	@Autowired
	private IOsOrderService orderService;
	@Autowired
	private IOsOrderStatusService orderStatusService;
	@Autowired
	private IOsOrderProductService orderProductService;
	@Autowired
	private IOsOrderShipmentService orderShipmentService;
	
	/**
	 * GET 版本日志
	 * @param model
	 * @return
	 */
	@GetMapping(value = { "", "/list" })
	public String list(Model model) {
		
		// 商品总数量
		Integer allGoodsNumber = productService.selectAllProductsNumber(null);
		model.addAttribute("allGoodsNumber", allGoodsNumber);
		
		//上架商品
		Integer onGoodsNumber = productService.selectAllProductsNumber(1);
		model.addAttribute("onGoodsNumber", onGoodsNumber);	
		
		//下架商品
		Integer outGoodsNumber = productService.selectAllProductsNumber(0);
		model.addAttribute("outGoodsNumber", outGoodsNumber);
		
		// 类目列表
		List<OsCategoryDto> osCategoryDtoList = categoryService.selectCategoryAndNumber(1);
		model.addAttribute("osCategoryDtoList", osCategoryDtoList);
				
		
		return SYSTEM_ORDER_LIST;
	}
	
	/**
	 * 产品列表
	 * @param model 
	 * @param pageInfo 分页条件
	 * @return
	 */
	@ResponseBody
	//@RequiresPermissions("system:goods:list")
	@PostMapping(value = {"/list/data" },produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public PageInfo lists(Model model, @RequestBody JSONObject jsonParam) {
		
		//获取查询下条件
		Integer limit = 0;
		Integer offset = 0;
		String sortStr = "createTime";
		String order ="desc";
		
		if(jsonParam.containsKey("offset")){
			offset = jsonParam.getInteger("offset");
		}
		
		if(jsonParam.containsKey("limit")){
			limit = jsonParam.getInteger("limit");
		}
		
		if(jsonParam.containsKey("sort")){
			sortStr = jsonParam.getString("sort")!=null && !jsonParam.getString("sort").equals("") ? jsonParam.getString("sort") : sortStr;
		}
		
		if(jsonParam.containsKey("order")){
			order = jsonParam.getString("order");
		}
		
		//设置查询条件
		PageInfo pageInfo = new PageInfo();
		pageInfo.initPageSet(offset, limit, sortStr, order);
		
		//查询数据
		pageInfo = orderService.selectByPullPage(pageInfo);
		
		
		return pageInfo;
	}
	
	
	
	/**
	 * POST 版本日志
	 * @param model 
	 * @param pageInfo 分页条件
	 * @return
	 */
	@RequiresPermissions("system:order:list")
	@PostMapping(value = {"/list" })
	public String list(Model model, PageInfo pageInfo) {
		VersionLogDTO versionLogDTO = versionLogService.selectByPullPage(pageInfo);
		model.addAttribute("versionLogs",versionLogDTO.getVersionLogs());
		model.addAttribute("pageInfo", versionLogDTO.getPageInfo());
		return SYSTEM_VERSION_PAGE;
	}
	
	
	//@RequiresPermissions("system:order:list:view")
	@GetMapping(value = {"/list/view/{id}" })
	public String view(Model model,@PathVariable("id") Long orderId ) {
		
		System.out.println(orderId);
		
		//订单基本信息
		OsOrder order = orderService.selectById(orderId);
		model.addAttribute("order", order);
		
		//订单状态
		EntityWrapper<OsOrderStatus> statusWrapper = new EntityWrapper<OsOrderStatus>();
		statusWrapper.where("order_id={0}",orderId); 
		List<OsOrderStatus> statusList = orderStatusService.selectList(statusWrapper);
		model.addAttribute("orderStatusList", statusList);
		
		//订单包含商品
		EntityWrapper<OsOrderProduct> productWrapper = new EntityWrapper<OsOrderProduct>();
		productWrapper.where("order_id={0}",orderId); 
		List<OsOrderProduct> productList = orderProductService.selectList(productWrapper);
		model.addAttribute("orderProductList", productList);
		
		//订单邮寄地址信息
		EntityWrapper<OsOrderShipment> shipmentWrapper = new EntityWrapper<OsOrderShipment>();
		shipmentWrapper.where("order_id={0}",orderId); 
		OsOrderShipment orderShipment = orderShipmentService.selectOne(shipmentWrapper);
		model.addAttribute("orderShipment", orderShipment);
		
		return SYSTEM_ORDER_LIST_VIEW;
	}
	
	@RequiresPermissions("system:order:list:view")
	@PostMapping(value = {"/list/view" })
	public String view(Model model, PageInfo pageInfo) {
		VersionLogDTO versionLogDTO = versionLogService.selectByPullPage(pageInfo);
		model.addAttribute("versionLogs",versionLogDTO.getVersionLogs());
		model.addAttribute("pageInfo", versionLogDTO.getPageInfo());
		return SYSTEM_VERSION_PAGE;
	}
	
	@RequiresPermissions("system:order:list:edit")
	@PostMapping(value = {"/list/edit" })
	public String edit(Model model, PageInfo pageInfo) {
		VersionLogDTO versionLogDTO = versionLogService.selectByPullPage(pageInfo);
		model.addAttribute("versionLogs",versionLogDTO.getVersionLogs());
		model.addAttribute("pageInfo", versionLogDTO.getPageInfo());
		return SYSTEM_VERSION_PAGE;
	}
	
	@RequiresPermissions("system:order:list:delete")
	@PostMapping(value = {"/list/delete" })
	public String delete(Model model, PageInfo pageInfo) {
		VersionLogDTO versionLogDTO = versionLogService.selectByPullPage(pageInfo);
		model.addAttribute("versionLogs",versionLogDTO.getVersionLogs());
		model.addAttribute("pageInfo", versionLogDTO.getPageInfo());
		return SYSTEM_VERSION_PAGE;
	}
	
	@RequiresPermissions("system:order:list:add")
	@PostMapping(value = {"/list/add" })
	public String add(Model model, PageInfo pageInfo) {
		VersionLogDTO versionLogDTO = versionLogService.selectByPullPage(pageInfo);
		model.addAttribute("versionLogs",versionLogDTO.getVersionLogs());
		model.addAttribute("pageInfo", versionLogDTO.getPageInfo());
		return SYSTEM_VERSION_PAGE;
	}
	
	
}
